package com.mall4j.cloud.coupon.controller.platform;

import com.mall4j.cloud.api.coupon.bo.CouponBO;
import com.mall4j.cloud.api.coupon.dto.CouponAllocateReq;
import com.mall4j.cloud.common.database.dto.PageDTO;
import com.mall4j.cloud.common.database.vo.PageVO;
import com.mall4j.cloud.common.response.ServerResponseEntity;
import com.mall4j.cloud.common.security.AuthUserContext;
import com.mall4j.cloud.coupon.model.CouponConfig;
import com.mall4j.cloud.coupon.service.CouponConfigService;
import com.mall4j.cloud.coupon.service.UserCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 优惠卷配置服务
 * @author ljh
 */
@RestController
@RequestMapping("/platform/coupon_config")
public class CouponConfigController {
    @Autowired
    private CouponConfigService couponConfigService;
    @Autowired
    private UserCouponService userCouponService;
    @GetMapping("/page")
    public ServerResponseEntity<PageVO<CouponConfig>> page(@Valid PageDTO pageDTO){
        return ServerResponseEntity.success(couponConfigService.page(pageDTO));
    }
    @PostMapping("/save")
    public ServerResponseEntity<Void> save(@RequestBody CouponConfig couponConfig) {
        couponConfigService.save(couponConfig);
        return ServerResponseEntity.success();
    }

    // 领取优惠卷
    @PostMapping("/applyCoupon")
    public ServerResponseEntity<Void> applyCoupon(@RequestBody CouponAllocateReq req) {
        long userId = AuthUserContext.get().getUserId();
        // TODO 校验
        userCouponService.allocateUserCoupon(req.getCouponConfigId(),req.getUserId(),"test");
        return ServerResponseEntity.success();
    }
}
